// pages/addstudent/index.js
import * as echarts from '../../ec-canvas/echarts.min';
import loginFn from '../../utils/login.js'
import urls from '../../utils/urls.js'
var app = getApp();
var option

function initChart(canvas, width, height) {
	var chart = echarts.init(canvas, null, {
		width: width,
		height: height
	});
	canvas.setChart(chart);
	chart.setOption(option);
	return chart;
}
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		sexIdx: 0, //默认性别
		sex: ['未知', '男', '女'], //性别
		date: '2014-01-01', //默认出生日期
		grade: [], //年级
		gradeIdx: 0, //默认年级
		school: '', //学校
		logoimg: '/img/addstudent.png', //头像
		studentImgId: 0, //头像id
		subBtn: false,
		studentName: "", //学生姓名
		studentId: 0, //学生id
		canedit: false, //是否可以编辑
		hastest: false, //是否测评过
		ec: {
			onInit: initChart,
			showchart: false, //显示chart
			studentId: 0, //学生id
			standard: [], //评分标准
			recommendList: [] //推荐课程
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		var that = this;
		var loginInfo = loginFn()
		var pages = getCurrentPages()
		var prepage = pages[pages.length - 2]
		var studentinfo = prepage.data.studentList[options.idx]

		wx.request({
			url: urls.studentGrade,
			data: loginInfo,
			success: function(res) {
				console.log(res)
				if (res.data.code == '0') {
					var gradeIdx = 0;
					for (var i = 0; i < res.data.data.grade.length; i++) {
						if (res.data.data.grade[i].gradeId == studentinfo.gradeId) {
							gradeIdx = i
						}
					}

					that.setData({
						grade: res.data.data.grade,
						gradeIdx: gradeIdx,
						sexIdx: studentinfo.sex, //默认性别
						date: studentinfo.birthday, //默认出生日期
						school: studentinfo.school, //学校
						logoimg: studentinfo.studentImg, //头像
						studentName: studentinfo.studentName,
						studentId: studentinfo.studentId
					})

				};
			}
		})


		//测评
		if (studentinfo.question.length > 0) {
			this.setData({
				hastest: true
			})
			this.getresult(studentinfo.studentId) //获取测试结果
		}

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	bindsexChange: function(e) { //选择性别
		this.setData({
			sexIdx: e.detail.value
		})
	},
	binddateChange: function(e) { //选择日期
		console.log(e)
		this.setData({
			date: e.detail.value
		})
	},
	bindgradeChange: function(e) { //选择年级
		this.setData({
			gradeIdx: e.detail.value
		})
	},
	saveimgFn: function(e) { //保存头像
		var that = this;
		wx.chooseImage({
			count: 1,
			sizeType: ['compressed'],
			sourceType: ['album', 'camera'],
			success(res) {
				var tempFilePaths = res.tempFilePaths;
				app.uploadImage(tempFilePaths[0], (res) => {
					if(res.code == 1) {
						that.setData({
							logoimg: tempFilePaths,
							studentImgId: res.data.imageFileId
						})
					} else {
						app.alert(res.msg);
					}
				})
			}
		})
	},
	saveFn: function(e) { //保存信息
		console.log(e)
		this.setData({
			subBtn: true
		})
		var that = this;
		var datas = e.detail.value;
		var cansave = true;
		for (var key in datas) {
			if (key == 'studentName' || key == 'school') {
				if (datas[key] == "") {
					wx.showToast({
						title: '请完善资料',
						icon: 'none'
					})
					cansave = false
					that.setData({
						subBtn: false
					})
					break
				};
			};
			if (key == 'gradeId') {
				datas[key] = this.data.grade[datas[key]].gradeId
			};
		}
		if (this.data.studentImgId > 0) {
			datas['studentImg'] = this.data.studentImgId
		}
		if (cansave) {
			var loginInfo = loginFn()
			for (var key in loginInfo) {
				datas[key] = loginInfo[key]
			}
			datas.studentId = this.data.studentId
			wx.request({
				url: urls.studentSave,
				data: datas,
				method: 'POST',
				success: function(res) {
					console.log(res)
					if (res.data.code == '0') {
						wx.showToast({
							title: '保存成功',
						})
						that.setData({
							canedit: false
						})

					};
				}
			})
		};
	},
	editFn: function() { //编辑
		this.setData({
			canedit: true
		})
	},
	//排序函数
	sortNumber(a, b) {
		return a.questionDimensionId - b.questionDimensionId;
	},
	getresult: function(studentId) { //获取测试结果
		var that = this;
		var loginInfo = loginFn()
		loginInfo.studentId = studentId
		wx.request({
			url: urls.showScore,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0') {
					for (var k = 0; k < res.data.data.length; k++) {
						if (res.data.data[k].studentId == studentId) {
							var results = res.data.data[k].score.sort(that.sortNumber)
							var names = []
							var scores = []
							for (var i = 0; i < results.length; i++) {
								names[i] = {
									"name": results[i].dimension,
									"max": 100
								}
								scores[i] = results[i].score
							}
							option = {
								backgroundColor: "#ffffff",
								color: ["#37A2DA"],
								tooltip: {},
								xAxis: {
									show: false
								},
								yAxis: {
									show: false
								},
								radar: {
									// shape: 'circle',
									indicator: names
								},
								series: [{
									name: '测评结果',
									type: 'radar',
									data: [{
										value: scores,
										name: '分数'
									}]
								}]
							};
							that.setData({
								showchart: true
							})
							console.log(res.data.data[k])
							that.showresult(res.data.data[k].score, studentId)
							break
						}
					}

				}
			}
		})

	},
	showresult: function(results, studentId) { //测评结果评分标准
		var that = this;
		var loginInfo = loginFn()
		//loginInfo.questionTypeId = 1
		loginInfo.studentId = studentId
		wx.request({
			url: urls.questionresul,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				console.log(res)
				if (res.data.code == '0') {
					var dimensionList = res.data.data.dimensionList
					//console.log(results)
					//console.log(dimensionList)
					for (var i = 0; i < results.length; i++) {
						for (var j = 0; j < dimensionList.length; j++) {
							if (results[i].questionDimensionId == dimensionList[j].questionDimensionId) {
								if (results[i].score > 0 && results[i].score <= 25) {
									results[i].result = dimensionList[j].score01
								} else if (results[i].score > 25 && results[i].score <= 50) {
									results[i].result = dimensionList[j].score12
								} else if (results[i].score > 50 && results[i].score <= 75) {
									results[i].result = dimensionList[j].score23
								} else if (results[i].score > 75 && results[i].score <= 100) {
									results[i].result = dimensionList[j].score34
								}
							}
						}
					}
					that.setData({
						standard: results,
						recommendList: res.data.data.recommendList.list
					})
				}
			}
		})
	},
	restestFn: function() { //重新测评
		var year = 1000 * 60 * 60 * 24 * 365;
		var now = new Date();
		var birth = this.data.date;
		birth = Date.parse(birth.replace('/-/g', "/"));
		var birthday = new Date(birth);
		var age = parseInt((now - birthday) / year);
		var questionTypeId = 1
		if (age > 6) {
			questionTypeId = 2
		};
		wx.navigateTo({
			url: '/pages/test/index?studentId=' + this.data.studentId + '&questionTypeId=' + questionTypeId,
		})
	}
})
